//
//  DestinationViewController.m
//  God_is_a_Girl
//
//  Created by 李继鹏 on 15/9/23.
//  Copyright (c) 2015年 呸. All rights reserved.
//

#import "DestinationViewController.h"
#import "DestinationViewController.h"
#import "DestinationModel.h"
#import "DestinationCollectionReusableView.h"
#import "LHQDestinationCollectionReusableView.h"
#import  <AFNetworking.h>
#import  "DestinationCollectionViewCell.h"
#import "LHQDestinationDetailViewController.h"
#import "LHQDestinationPhotoViewController.h"
#define WIDTH ([UIScreen mainScreen].bounds.size.width/375)
#define HEIGH ([UIScreen mainScreen].bounds.size.height/667
@interface DestinationViewController () <UICollectionViewDataSource,UICollectionViewDelegate>
@property (nonatomic,retain) UICollectionViewFlowLayout  *flowLayout;
@property (nonatomic,retain) UICollectionView  *collection;
@property (nonatomic,retain) NSArray  *arr;//data
@property (nonatomic,retain) NSMutableArray  *mArray;
@property (nonatomic,retain) NSString  *type;

@end
@implementation DestinationViewController
-(void)viewWillAppear:(BOOL)animated
{
   self.tabBarController.tabBar.alpha = 1;
   self.tabBarController.tabBar.frame = CGRectMake(0, [UIScreen mainScreen].bounds.size.height-49, self.view.frame.size.width, 49);

}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self  dataHandel];
    self.navigationController.navigationBar.translucent = NO;

}
- (void)collectViewSubView

{   //创建瀑布流
    self.flowLayout = [[UICollectionViewFlowLayout  alloc] init];
    //创建对象
    self.collection = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) collectionViewLayout:self.flowLayout];
    //方块大小
    self.flowLayout.itemSize = CGSizeMake(335*WIDTH/2, 150);
    self.flowLayout.headerReferenceSize = CGSizeMake(self.view.frame.size.width,70);
    //最小列间距
    self.flowLayout.minimumInteritemSpacing = 10 ;
    //最小行间距
    self.flowLayout.minimumLineSpacing = 10 ;
    //设置item据四边的间距
    self.flowLayout.sectionInset = UIEdgeInsetsMake(15 , 15 , 15 , 15);
    [self.view  addSubview:self.collection];
    self.collection.backgroundColor = [UIColor whiteColor];
    //两个协议
    self.collection.delegate = self;
    self.collection.dataSource = self;
    
    //collectionViewcell 必须提前注册
    [self.collection registerClass:[DestinationCollectionViewCell  class] forCellWithReuseIdentifier:@"reuse"];
    //标题
    [self.collection  registerClass:[DestinationCollectionReusableView  class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"sectionTitle"];
    //更多
    [self.collection  registerClass:[LHQDestinationCollectionReusableView  class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"sectionTitleButton"];
    
}
//数据解析
-(void)dataHandel
{
    self.arr  = [NSArray array];
    self.CArr = [NSMutableArray  array];
    AFHTTPRequestOperationManager  *manager = [AFHTTPRequestOperationManager   manager];
    
    [ manager  GET:@"http://api.breadtrip.com/destination/v3/?last_modified_since=0" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSArray  *arr1 = [NSArray  array];
        arr1 = [responseObject objectForKey:@"elements"];
       
        self.arr = [[arr1  objectAtIndex:0] objectForKey:@"data"];//图片和名字
        self.mArray = [NSMutableArray arrayWithArray:arr1];//标题
        //求index
        for (NSDictionary *dic in arr1) {
            NSString  *str = [ dic objectForKey:@"index"] ;
           [self.CArr addObject:str];
        }
        [self collectViewSubView];
        NSLog(@"%ld", self.CArr.count);
        [self.collection  reloadData];
    
       } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
    
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    if (self.mArray.count == 0) {
        return 0;
    }
    else{
        return self.mArray.count;
    }
}
//每个分组有几个cell
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (self.arr.count == 0) {
        return 0;
    
    }else{
        
        if (section == 4) {
            return 2;
        }
        else{
            return self.arr.count ;
        }
        
    }
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary  *dic = [[[self.mArray objectAtIndex:indexPath.section]objectForKey:@"data"]objectAtIndex:indexPath.row];
    
    
    DestinationModel  *model = [[DestinationModel  alloc] init];
    
    [model  setValuesForKeysWithDictionary:dic];
    
    DestinationCollectionViewCell  *cell = [collectionView  dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    
    cell.model = model;
    
    return cell;
    
}

//设置头
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.section == 2 || indexPath.section == 5 || indexPath.section == 6) {
        LHQDestinationCollectionReusableView  *view = [collectionView  dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"sectionTitleButton" forIndexPath:indexPath];
        DestinationModel  *model  = [[DestinationModel  alloc] init];
        NSMutableDictionary *mDic = [NSMutableDictionary dictionary];
        mDic = _mArray[indexPath.section];
        [model   setValuesForKeysWithDictionary:mDic];
        view.model = model;
        view.button.tag = indexPath.section + 100;
        [view.button addTarget:self action:@selector(DetailAction:) forControlEvents:UIControlEventTouchUpInside];
        return view;
        
    }else{
        DestinationCollectionReusableView  *view = [collectionView  dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"sectionTitle" forIndexPath:indexPath];
        DestinationModel  *model  = [[DestinationModel  alloc] init];
        NSMutableDictionary *mDic = [NSMutableDictionary dictionary];
        mDic = _mArray[indexPath.section];
        [model   setValuesForKeysWithDictionary:mDic];
        view.model = model;
        return view;
    }
    
}
//section距离
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    return CGSizeMake(self.view.frame.size.width, 20);
}
//跳转详情界面
-(void)DetailAction:(UIButton *)button
{
    
    
    if (button.tag == 2 + 100) {
       LHQDestinationPhotoViewController  *detail = [[LHQDestinationPhotoViewController  alloc] init];
        detail.type = self.type;
        detail.number = [_CArr[2] integerValue];
       [self.navigationController  pushViewController:detail animated:YES];
    }

     if (button.tag == 5 + 100) {
        LHQDestinationPhotoViewController  *detail = [[LHQDestinationPhotoViewController  alloc] init];
        detail.type = self.type;
        detail.number = [_CArr[5] integerValue];
        [self.navigationController  pushViewController:detail animated:YES];
        
    }

    if (button.tag == 6 + 100)  {
        LHQDestinationPhotoViewController  *detail = [[LHQDestinationPhotoViewController  alloc] init];
        detail.type = self.type;
       detail.number = [_CArr[6] integerValue];
   [self.navigationController  pushViewController:detail animated:YES];
    }
    
}
    

//选择跳转详情
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    self.tabBarController.tabBar.alpha = 0;
    self.tabBarController.tabBar.frame = CGRectMake(-100, -100, 0, 0);
    
    LHQDestinationDetailViewController  *detailTotal = [[LHQDestinationDetailViewController alloc] init];
    NSMutableArray  *arr = [[self.mArray  objectAtIndex:indexPath.section]objectForKey:@"data"];
    self.str = [[arr  objectAtIndex:indexPath.row]objectForKey:@"id"];
    self.type = [[arr objectAtIndex:indexPath.row ]objectForKey:@"type"];
    detailTotal.type = self.type;
    detailTotal.detailStr = self.str;
    [self.navigationController  pushViewController:detailTotal animated:YES];

}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
